package com.lbx.controller;

import com.lbx.dao.UserDao;
import com.lbx.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 吕兵星
 * @date 2020/10/26 15:55
 */
@Controller
public class LoginController {
    @PostMapping("/login")
    public String login(@RequestParam("email") String email,
                        @RequestParam("password")String password,
                        Model model, HttpServletRequest request){
        System.out.println(email +"|||"+password);
        if ("123".equals(email)&& "111".equals(password)){
            Map<String,String> loginInfo = new HashMap<>(2);
            loginInfo.put("email","吕冰星");
            loginInfo.put("password",password);
            request.getSession().setAttribute("loginInfo",loginInfo);
            return "redirect:/main";
        }
        model.addAttribute("msg","用户名或者密码错误");
        return "index";
    }

    @GetMapping("/logout")
    public String logout(HttpServletRequest request){
         request.getSession().removeAttribute("loginInfo");
        return "index";
    }
    @Autowired
    private UserDao userDao;
    @GetMapping("/toUserInfo")
    public String toUserInfo(Model  model){
        List<User> users = userDao.getUsers();
        if (!users.isEmpty()){
            model.addAttribute("users",users);
        }
        return "userinfo";
    }
}
